package com.kun.service;

import cn.hutool.core.bean.BeanUtil;
import com.kun.domain.Member;
import com.kun.domain.MemberExample;
import com.kun.exception.BusinessException;
import com.kun.exception.BusinessExceptionEnum;
import com.kun.mapper.MemberMapper;
import com.kun.req.MemberLoginReq;
import com.kun.req.MemberRegisterReq;
import com.kun.req.MemberSendCodeReq;
import com.kun.resp.MemberLoginResp;
import com.kun.util.JwtUtil;
import com.kun.util.SnowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: MemberService
 * @Description: ...
 * @author: kunkun
 * @date: 2025/7/9 13:08
 */
@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;

    private Logger LOG = LoggerFactory.getLogger(MemberService.class);

    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    public Long register(MemberRegisterReq registerReq){
        Member memberDb = getMemberByMobile(registerReq.getMobile());
        if (memberDb!=null){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_PHONE_EXIST);
        }
        Member member = new Member();
//        member.setId(System.currentTimeMillis());
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(registerReq.getMobile());
        memberMapper.insert(member);
        return member.getId();
    }



    public void sendCode(MemberSendCodeReq memberSendCodeReq){
        //1. 查询是否注册过
        Member memberDb = getMemberByMobile(memberSendCodeReq.getMobile());
        if (memberDb==null){
            //1.1 未注册过，进行注册
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(memberSendCodeReq.getMobile());
            memberMapper.insert(member);
            LOG.info("未注册过，进行注册,{}",member.getMobile());
        }else{
            //1.2 注册过
            LOG.info("该手机号已经被注册");
        }
        //2. 生成验证码
//        String code = RandomUtil.randomString(4);
        String code = "8888";
        LOG.info("短信验证码：{}",code);
        //3. 保存短信记录表（手机号、短信验证码、有效期、是否已使用、业务类型、发送时间、使用时间）
        LOG.info("保存短信记录表（手机号、短信验证码、有效期、是否已使用、业务类型、发送时间、使用时间）");
        //4. 对接短信通道，发送验证码
        LOG.info("对接短信通道，发送验证码");
    }

    public MemberLoginResp login(MemberLoginReq loginReq){
        //1. 判断手机号是否存在，若手机号不存在，抛出异常
        Member memberDb = getMemberByMobile(loginReq.getMobile());
        if (memberDb==null){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_PHONE_NOT_EXIST);
        }
        //2. 判断验证码是否正确，若不正确，抛出异常
        if (!"8888".equals(loginReq.getCode())){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_CODE_ERROR);
        }
        //3. 定义resp返回类，返回结果
        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDb, MemberLoginResp.class);
        String key = "kunkun-12306";
        String token = JwtUtil.createToken(memberLoginResp.getId(),memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }


    private Member getMemberByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
}
